package com.lefevre.cms.web.action.common;


import com.lefevre.cms.bean.ErrorView;
import com.lefevre.cms.bean.follow.Follow;
import com.lefevre.cms.bean.follow.Follower;
import com.lefevre.cms.bean.message.Remind;
import com.lefevre.cms.bean.setting.SystemSetting;
import com.lefevre.cms.bean.user.AccessUser;
import com.lefevre.cms.bean.user.ResourceEnum;
import com.lefevre.cms.bean.user.User;
import com.lefevre.cms.service.follow.FollowService;
import com.lefevre.cms.service.message.RemindService;
import com.lefevre.cms.service.setting.SettingService;
import com.lefevre.cms.service.theme.ThemeService;
import com.lefevre.cms.utils.Base64;
import com.lefevre.cms.utils.JsonUtils;
import com.lefevre.cms.utils.RefererCompare;
import com.lefevre.cms.utils.WebUtil;
import com.lefevre.cms.utils.threadLocal.AccessUserThreadLocal;
import com.lefevre.cms.web.action.AccessDeviceUtil;
import com.lefevre.cms.web.action.CSRFTokenManage;
import com.lefevre.cms.web.action.follow.FollowManage;
import com.lefevre.cms.web.action.follow.FollowerManage;
import com.lefevre.cms.web.action.message.RemindManage;
import com.lefevre.cms.web.action.user.RoleAnnotation;
import com.lefevre.cms.web.action.user.UserManage;
import com.lefevre.cms.web.taglib.Configuration;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 关注接收表单
 */
@Controller
@RequestMapping("user/control/follow")
public class FollowFormAction {
    @Resource
    ThemeService themeService;

    @Resource
    RemindService remindService;
    @Resource
    RemindManage remindManage;
    @Resource
    UserManage userManage;
    @Resource
    FollowService followService;
    @Resource
    AccessDeviceUtil accessDeviceUtil;
    @Resource
    FollowManage followManage;
    @Resource
    FollowerManage followerManage;
    @Resource
    CSRFTokenManage csrfTokenManage;
    @Resource
    SettingService settingService;

    /**
     * 关注   添加
     *
     * @param model
     * @param userName 用户名称
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @RoleAnnotation(resourceCode = ResourceEnum._5001000)
    public String add(ModelMap model, String userName, String token, String jumpUrl,
                      RedirectAttributes redirectAttrs,
                      HttpServletRequest request, HttpServletResponse response) throws Exception {

        boolean isAjax = WebUtil.submitDataMode(request);


        Map<String, String> error = new HashMap<>();

        SystemSetting systemSetting = settingService.findSystemSetting_cache();
        if (systemSetting.getCloseSite().equals(2)) {
            error.put("follow", ErrorView._21.name());//只读模式不允许提交数据
        }

        //判断令牌
        if (token != null && !"".equals(token.trim())) {
            String token_sessionid = csrfTokenManage.getToken(request);//获取令牌
            if (token_sessionid != null && !"".equals(token_sessionid.trim())) {
                if (!token_sessionid.equals(token)) {
                    error.put("token", ErrorView._13.name());//令牌错误
                }
            } else {
                error.put("token", ErrorView._12.name());//令牌过期
            }
        } else {
            error.put("token", ErrorView._11.name());//令牌为空
        }

        //获取登录用户
        AccessUser accessUser = AccessUserThreadLocal.get();

        User user = null;
        if (userName != null && !"".equals(userName.trim())) {
            user = userManage.query_cache_findUserByUserName(userName.trim());//查询缓存
        } else {
            error.put("follow", ErrorView._815.name());//用户名称不能为空
        }

        if (user != null) {
            //关注Id
            String followId = followManage.createFollowId(accessUser.getUserId(), user.getId());

            Follow follow = followManage.query_cache_findById(followId);

            if (follow != null) {
                error.put("follow", ErrorView._1820.name());//当前用户已关注对方
            }

            if (user.getId().equals(accessUser.getUserId())) {
                error.put("follow", ErrorView._1870.name());//不能关注自身
            }

        } else {
            error.put("follow", ErrorView._859.name());//用户不存在
        }

        if (error.size() == 0) {
            Date time = new Date();
            Follow follow = new Follow();
            follow.setId(followManage.createFollowId(accessUser.getUserId(), user.getId()));
            follow.setAddtime(time);
            follow.setUserName(accessUser.getUserName());
            follow.setFriendUserName(user.getUserName());

            Follower follower = new Follower();
            follower.setId(followerManage.createFollowerId(user.getId(), accessUser.getUserId()));
            follower.setAddtime(time);
            follower.setUserName(user.getUserName());
            follower.setFriendUserName(accessUser.getUserName());
            try {
                //保存关注
                followService.saveFollow(followManage.createFollowObject(follow), followerManage.createFollowerObject(follower));

                //删除关注缓存
                followManage.delete_cache_findById(follow.getId());
                followerManage.delete_cache_followerCount(user.getUserName());
                followManage.delete_cache_findAllFollow(accessUser.getUserName());
                followManage.delete_cache_followCount(accessUser.getUserName());

                if (user != null && !user.getId().equals(accessUser.getUserId())) {//会员关注自已不发提醒给自己

                    //提醒对方
                    Remind remind = new Remind();
                    remind.setId(remindManage.createRemindId(user.getId()));
                    remind.setReceiverUserId(user.getId());//接收提醒用户Id
                    remind.setSenderUserId(accessUser.getUserId());//发送用户Id
                    remind.setTypeCode(80);//80.别人关注了我
                    remind.setSendTimeFormat(time.getTime());//发送时间格式化


                    Object remind_object = remindManage.createRemindObject(remind);
                    remindService.saveRemind(remind_object);

                    //删除提醒缓存
                    remindManage.delete_cache_findUnreadRemindByUserId(user.getId());
                }

            } catch (org.springframework.orm.jpa.JpaSystemException e) {
                error.put("follow", ErrorView._1800.name());//重复关注

            }

        }

        Map<String, String> returnError = new HashMap<>();
        if (error.size() > 0) {
            //将枚举数据转为错误提示字符
            for (Map.Entry<String, String> entry : error.entrySet()) {
                if (ErrorView.get(entry.getValue()) != null) {
                    returnError.put(entry.getKey(), ErrorView.get(entry.getValue()));
                } else {
                    returnError.put(entry.getKey(), entry.getValue());
                }
            }
        }
        if (isAjax) {

            Map<String, Object> returnValue = new HashMap<>();

            if (error.size() > 0) {
                returnValue.put("success", "false");
                returnValue.put("error", returnError);
            } else {
                returnValue.put("success", "true");
            }
            WebUtil.writeToWeb(JsonUtils.toJSONString(returnValue), "json", response);
            return null;
        } else {


            if (error.size() > 0) {
                redirectAttrs.addFlashAttribute("error", returnError);//重定向传参


                String referer = request.getHeader("referer");


                referer = StringUtils.removeStartIgnoreCase(referer, Configuration.getUrl(request));//移除开始部分的相同的字符,不区分大小写
                referer = StringUtils.substringBefore(referer, ".");//截取到等于第二个参数的字符串为止
                referer = StringUtils.substringBefore(referer, "?");//截取到等于第二个参数的字符串为止

                String queryString = request.getQueryString() != null && !"".equals(request.getQueryString().trim()) ? "?" + request.getQueryString() : "";

                return "redirect:/" + referer + queryString;

            }


            if (jumpUrl != null && !"".equals(jumpUrl.trim())) {
                String url = Base64.decodeBase64URL(jumpUrl.trim());

                return "redirect:" + url;
            } else {//默认跳转
                model.addAttribute("message", "关注该用户成功");
                String referer = request.getHeader("referer");
                if (RefererCompare.compare(request, "login")) {
                    referer = Configuration.getUrl(request);
                }
                model.addAttribute("urlAddress", referer);

                String dirName = themeService.findTemplateDir_cache();


                return "/templates/" + dirName + "/" + accessDeviceUtil.accessDevices(request) + "/jump";
            }
        }
    }


}
